package day_2022_7_to_8.day48;

import java.util.PriorityQueue;

/**
 * @author haomin
 * @date 2022/09/02 21:58
 **/
public class Day48_6_Offer40_GetLeastNumbers {
    class Solution {
        public int[] getLeastNumbers(int[] arr, int k) {
            if(arr.length == 0 || k == 0) return new int[0];
            PriorityQueue<Integer> qu = new PriorityQueue<>((o1, o2) -> o2 - o1);
            for (int i = 0; i < arr.length; ++i) {
                if(qu.size() < k) qu.offer(arr[i]);
                else if(arr[i] < qu.peek()){
                    qu.poll();qu.offer(arr[i]);
                }
            }
            int[] res =new int[qu.size()];
            for (int i = 0; i < res.length; i++) {
                res[i] = qu.poll();
            }
            return res;
        }
    }
}